import StageScene from "./StageScene"
import Map from "./Map"

export default class UIMoveScript extends Laya.Script3D {
  private camera: Laya.Camera
  private speed = 0.01
  private ray : Laya.Ray
  private   outHitResult : Laya.HitResult
  private point : Laya.Vector2
  onEnable() {
    console.log('控制界面移动')
    this.camera = this.owner as Laya.Camera

     // 创建射线
     this.ray = new Laya.Ray(new Laya.Vector3(0, 0, 0), new Laya.Vector3(0, 0, 0))
     this.point = new Laya.Vector2
     this.outHitResult = new Laya.HitResult

     this.camera.transform.translate(new Laya.Vector3(0, 0, -20), true) 
  }

  onUpdate() {
    this.move()
    // this.rayHit()
  }

  public rayHit(){
    this.point.x = Laya.MouseManager.instance.mouseX
    this.point.y = Laya.MouseManager.instance.mouseY
    this.camera.viewportPointToRay(this.point, this.ray)
    StageScene.instance.stageScene.physicsSimulation.rayCast(this.ray, this.outHitResult, 1000, Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER9, Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER9)
    if(this.outHitResult && this.outHitResult.succeeded) {
      // console.log(this.outHitResult.collider.owner.name)
      // 添加武器
      let map = this.outHitResult.collider.owner.getComponent(Map) as Map
      map.addWeapon()
    }
  }



  private move() {
    let speed = Laya.timer.delta * this.speed
    if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.A)) {   
      this.camera.transform.translate(new Laya.Vector3(speed, 0, 0), true) 
    } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.D)) {   
      this.camera.transform.translate(new Laya.Vector3(-speed, 0, 0), true) 
    }else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.W)) {   
      this.camera.transform.translate(new Laya.Vector3(0, 0, speed), true) 
    }else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.S)) {   
      this.camera.transform.translate(new Laya.Vector3(0, 0, -speed), true) 
    } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.Q)) {   
      this.camera.transform.translate(new Laya.Vector3(0, -speed, 0), true) 
    }else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.E)) {   
      this.camera.transform.translate(new Laya.Vector3(0, speed, 0), true) 
    }
  }
}